package com.sakiko.home3.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

// spring 2.5 版本的配置
// 推荐自己写的类分类别，使用 @Component、@Controller、@Service、@Repository注解
// 第三方框架类使用配置类 @Bean return bean;
@Configuration // 标识 配置类
@ComponentScan(basePackages = {"com.sakiko.home3"}) // 注解扫描
@PropertySource(value = {"classpath:properties/jdbc.properties"}) // 资源文件读取
public class CustomBeanConfig {

    @Bean
    public DruidDataSource dataSource(@Value("${jdbc.driver}") String driverClassName,
                                      @Value("${jdbc.url}") String url,
                                      @Value("${jdbc.username}") String username,
                                      @Value("${jdbc.password}") String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
